<?php
import("ORG.Util.Function");
import("@.Org.Utils");
import("@.Org.WholesaleTool");

class WholesaleAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	/**
	 * 批发列表
	 * Enter description here ...
	 */
	public function wholesale_list(){
	   		
//	    $sort_flag  				= 	sort_flag($list['filter']);
//	    $this->assign($sort_flag['tag'], $sort_flag['img']); // 默认排序字段
		
	    $sql_wholesale_list 		= 	get_sql_wholesale_list();
	    $param = array(
				'result'	=> $sql_wholesale_list,			//分页用的数组或sql
				'listvar'	=> 'wholesale_list',				//分页循环变量
				'listRows'	=> 10,							//每页记录数
				//'parameter'=>'cat_id=' . $cat_id . "&intro_type=" . $intro_type . "&invalid=" . $invalid,//url分页后继续带的参数
				'target'	=> 'content',	    			//ajax更新内容的容器id，不带#
				'pagesId'	=> 'page',						//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
				'template'	=> 'Wholesale:wholesale_list',		//ajax更新模板
		);	
		$this->page_wholesale_list($param);
		$this->display('wholesale_list');
	}
	
	/**
	 * 批发列表分页
	 * Enter description here ...
	 * @param unknown_type $param
	 */
	public function page_wholesale_list($param){
		extract($param);
		//总记录数
		$flag = is_string($result);
		
		$listvar = $listvar ? $listvar : 'list';
		$listRows = $listRows? $listRows : 21;
		if ($flag){
			$totalRows	=	count(M()->query($result));
		}			
		else{
			$totalRows = ($result) ? count($result) : 1;
		}
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');//要ajax分页配置PAGE中必须theme带%ajax%，其他字符串替换统一在配置文件中设置，
		//===========================================================格式化数据
		$CommonModel		=	new CommonModel();
	    /* 查询会员等级 */
	    $rank_list 			= 	array();
	    $sql 				= 	"SELECT rank_id, rank_name FROM " . 'ybr_user_rank';
	    $res 				= 	$CommonModel->query($sql);
	    
	    foreach($res as $row){
	    	$rank_list[$row['rank_id']] = $row['rank_name'];
	    }
	    $list 		= 	array();
	    
	    foreach($voList as $row){
	    	$rank_name_list 				= 	array();
	        if ($row['rank_ids']){
	            $rank_id_list 				= 	explode(',', $row['rank_ids']);
	            foreach ($rank_id_list as $id){
	                if (isset($rank_list[$id])){
	                    $rank_name_list[] 	= 	$rank_list[$id];
	                }
	            }
	        }
	        $row['rank_names'] 				= 	join(',', $rank_name_list);
	        $row['price_list'] 				= 	unserialize($row['prices']);	
	        $list[] 						= 	$row;
	    }    
	    $voList		=	$list;
		//===========================================================格式化数据
		//可以使用该方法前用C临时改变配置
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();		
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);
		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'ajaxlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}
	
	/**
	 * 添加
	 * Enter description here ...
	 */
	public function add_wholesale(){
		
		$GoodsModel			=	new GoodsModel();
		
	    $this->assign('form_action', 'insert');	
	    /* 初始化、取得批发活动信息 */
        $wholesale = array(
            'act_id'        => 0,
            'goods_id'      => 0,
            'goods_name'    => $_LANG['pls_search_goods'],
            'enabled'       => '1',
            'price_list'    => array()
        );
	  
	    if (empty($wholesale['price_list'])){
	        $wholesale['price_list'] = array(
	            array(
	                'attr'    => array(),
	                'qp_list' => array(
	                    array('quantity' => 0, 'price' => 0)
	                )
	            )
	        );
	    }
	    $this->assign('wholesale', $wholesale);
	
	    /* 取得用户等级 */
	    $user_rank_list 		= 	array();
	    $sql 					= 	"SELECT rank_id, rank_name FROM " . 'ybr_user_rank' .
	            					" ORDER BY special_rank, min_points";
	    $res 					= 	M()->query($sql);
	    
	    foreach($res as $rank){
	    	if (!empty($wholesale['rank_ids']) && strpos($wholesale['rank_ids'], $rank['rank_id']) !== false){
	            $rank['checked'] = 1;
	        }
	        $user_rank_list[] = $rank;
	    }
	    
	    $this->assign('user_rank_list', $user_rank_list);	
	    $this->assign('cat_list', 		$GoodsModel->cat_list());
	    $this->assign('brand_list',   	get_brand_list());
			
		$this->display('wholesale_info');
	}
	
	
	public function mod_wholesale(){
		
		$this->display();
	}
	
	
	
}









